""" Mandelbrot Process """
from common import *

from numpy import ogrid, zeros, abs, isnan, ones, complex

default_data = 50

def setup(data = default_data):
    import wx
    dlg = wx.TextEntryDialog(
        None, 'Number of iterations',
        'Configure Mandelbrot Process', str(data))
    if dlg.ShowModal() == wx.ID_OK:
        try:
            data = eval(dlg.GetValue())
        except:
            data = dlg.GetValue()
    dlg.Destroy()
    return data

def MandelbrotFunc(in0,in1,out0 , data = default_data):
    while True:
        (x1,x2,size_x) = in0()
        (y1,y2,size_y) = in1()

        x, y = ogrid[x1:x2:(size_x*1j), y1:y2:(size_y*1j)]
        c = x + y * 1j
        threshold_time = zeros((size_x, size_y))
        z = zeros((size_x, size_y), complex)
        mask = ones((size_x, size_y), dtype=bool)
        for i in range(data):
            z[mask] = z[mask]**2 + c[mask]
            mask = (abs(z) < 10)
            threshold_time += mask

        out0(threshold_time)

